package com.my.job.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.job.common.Page;
import com.my.job.domain.CronTriggers;
import com.my.job.domain.Triggers;
import com.my.job.service.QuartzService;
import com.my.job.service.SchedulerService;
import com.my.job.view.TriggerVO;

@Controller
@RequestMapping("/job")
public class JobWebController
{

	@Autowired
	private QuartzService quartzService;
	
	@Autowired
	private SchedulerService schedulerService;
	
	@RequestMapping("/list")
	public String getList(TriggerVO vo,@RequestParam(defaultValue="1",required=false)Integer p,Model model)
	{
		Triggers po = new Triggers();
		this.voToPo(vo, po);
		
		Page<Triggers> page = new Page<Triggers>(10);
		page.setPageNo(p);
		page = schedulerService.list(po, page);
		
		List<Triggers> list = page.getResult();
		
		if(CollectionUtils.isEmpty(list))
		{
			return null;
		}
		
		List<TriggerVO> voList = new ArrayList<TriggerVO>();
		for(Triggers pojo : list)
		{
			TriggerVO v = new TriggerVO();
			this.poToVo(pojo, v);
			voList.add(v);
		}
		
		
		model.addAttribute("triggerList", voList);
		
		return "/job/list";
	}

	@ResponseBody
	@RequestMapping("/cron")
	public Map<String, Object> getCronExpression(CronTriggers po)
	{
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("cronExpression", schedulerService.getCron(po));
		
		return modelMap;
	}

	@ResponseBody
	@RequestMapping("/update")
	public Map<String, Object> updateCronExpression(@RequestParam String triggerGroup, @RequestParam String triggerName, @RequestParam String cronExpression)
	{

		boolean result = quartzService.updateCronExpression(triggerGroup, triggerName, cronExpression);

		Map<String, Object> modelMap = new HashMap<String, Object>();

		modelMap.put("result", result);

		return modelMap;
	}

	@ResponseBody
	@RequestMapping("/operate")
	public Map<String, Object> operation(@RequestParam String operate, @RequestParam String triggerGroup, @RequestParam String triggerName)
	{

		boolean flag = false;

		// 暂停
		if ("pause".equals(operate))
		{
			flag = quartzService.pauseTrigger(triggerName, triggerGroup);
		}
		// 恢复
		else if ("resume".equals(operate))
		{
			flag = quartzService.resumeTrigger(triggerName, triggerGroup);
		}
		// 执行
		else if ("execute".equals(operate))
		{
			flag = quartzService.runJob(triggerName, triggerGroup);
		}
		else if("delete".equals(operate))
		{
			flag = quartzService.deleteJob(triggerName, triggerGroup);
		}

		Map<String, Object> modelMap = new HashMap<String, Object>();

		modelMap.put("result", flag);

		return modelMap;
	}
	
	private void poToVo(Triggers po,TriggerVO vo)
	{
		BeanUtils.copyProperties(po, vo);
		if(po.getNextFireTime()>0)
		{
			vo.setNextFireTimeStr(DateFormatUtils.format(po.getNextFireTime(), "yyyy-MM-dd HH:mm:ss"));
		}
		if(po.getPrevFireTime()>0)
		{
			vo.setPrevFireTimeStr(DateFormatUtils.format(po.getPrevFireTime(), "yyyy-MM-dd HH:mm:ss"));
		}
		if(po.getStartTime()>0)
		{
			vo.setStartTimeStr(DateFormatUtils.format(po.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
		}
		if(po.getEndTime()>0)
		{
			vo.setEndTimeStr(DateFormatUtils.format(po.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
		}
	}
	
	private void voToPo(TriggerVO vo,Triggers po)
	{
		BeanUtils.copyProperties(vo, po);
	}

}
